package com.qtummatrix.controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.qtummatrix.entity.BlackList;
import com.qtummatrix.service.BlacklistService;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("findBlacklist")
public class BlacklistController extends ActionSupport implements ModelDriven<BlackList> {
    @Autowired
    private BlacklistService blacklistService;

    Map<String, Object> info = new HashMap<String, Object>();


    public Map<String, Object> getInfo() {
        return info;
    }


    public void setInfo(Map<String, Object> info) {
        this.info = info;
    }

    private BlackList blackList = new BlackList();


    public BlackList getBlackList() {
        return blackList;
    }


    public void setBlackList(BlackList blackList) {
        this.blackList = blackList;
    }

    @Override
    public BlackList getModel() {
        return blackList;
    }

    //当前页码
    private Integer pageNo;
    //每页记录数
    private Integer pageSize;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String findBlacklist() {
        String UID = blackList.getUID();
        String name = blackList.getName();
        String source = blackList.getSource();
        String state = blackList.getState();
        HttpServletRequest request = ServletActionContext.getRequest();

        String page = request.getParameter("pageNo");//当前页数
        String rows = request.getParameter("pageSize");//当前条数

        System.out.println(rows);
        List<BlackList> list = null;


        //int page=total%Integer.parseInt(rows)+1;
        list = blacklistService.findBlacklist(UID, name, source, state,
                (Integer.parseInt(page) - 1) * Integer.parseInt(rows), Integer.parseInt(rows));
        List<BlackList> list1 = blacklistService.findBlacklist();
        int total = list1.size();//总条数

        //总页数
        int pages = total % Integer.parseInt(rows) == 0 ? total / Integer.parseInt(rows)
                : total / Integer.parseInt(rows) + 1;
        info.put("list", list);

        info.put("pageNum", page);//当前页数
        info.put("pages", pages);//总页数
        info.put("total", total);//总条数
        System.out.println(list);
        return SUCCESS;
    }
}
